package com.guigu.servicegoods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicegoods.domain.Categorys;
import com.guigu.servicegoods.domain.TbCategory;
import com.guigu.servicegoods.service.TbCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品类目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/servicegoods/category")
public class TbCategoryController {

    @Autowired
    private TbCategoryService categoryService;

    // 封装三级分类
    @GetMapping("selectCategorys")
    public Result selectCategorys(){
        List<Categorys> categorys = categoryService.selectCategorys();
        return Result.ok().data("categorys",categorys);
    }

    // 分页条件查询
    @PostMapping("findPage")
    public Result findPage(@RequestBody(required = false) Map<String, Object> searchMap, Integer page, Integer size){
        return categoryService.findPage(searchMap,page,size);
    }

    /**
     * 根据ID获取品牌
     * @param id
     * @return
     */
    @GetMapping("findByIdBrand")
    public Result findByIdBrand(Integer id){
        return categoryService.findByIdBrand(id);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody TbCategory category) {
        categoryService.insert(category);
        return Result.ok();
    }

    @PostMapping("/update")
    public Result update(@RequestBody TbCategory category) {
        categoryService.ByIdupdate(category);
        return Result.ok();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        QueryWrapper<TbCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<TbCategory> list = categoryService.list(wrapper);
        if (list.size() > 0) {
            return Result.error();
        } else {
            categoryService.ByIdremove(id);
            return Result.ok();

        }
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        TbCategory category = categoryService.getById(id);
        if (category==null){
            return Result.error();
        }
        return Result.ok().data("category",category);
    }

    @GetMapping("findCategoryTree")
    public List<Map> findCategoryTree(){
        return categoryService.findCategoryTree();
    }

    @GetMapping("/findById2")
    public TbCategory findById2(Integer id) {
        return categoryService.getById(id);
    }
}

